package com.fujimana.android.manacode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fujimana.android.manacode.TreeView.OnTreeItemClickListener;
import com.fujimana.android.manacode.loalder.LoaderFactory;
import com.fujimana.android.manacode.loalder.Sources;
import com.fujimana.android.manacode.loalder.Tree;

public class SourceTreeListener implements TabListener {
	
	private LinearLayout layout;
	
	private ManacodeActivity activity;
	
	private Sources archive;
	
	private Tree<String> tree;
	
	private String id;

	SourceTreeListener(LinearLayout layout, ManacodeActivity activity, Sources archive, String path) {
		this.layout = layout;
		this.activity = activity;
		this.archive = archive;
		this.id = path;
	}
	
	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
	
		
		LayoutInflater inf = this.activity.getLayoutInflater();
		inf.inflate(R.layout.source_tree, this.layout);
		TreeView treeView = (TreeView) this.layout.findViewById(R.id.treeView1);
		
		TextView textView = (TextView) this.layout.findViewById(R.id.tree_title);
		textView.setText(this.id);
		
		if(this.tree == null){
			this.tree = LoaderFactory.getInstance().getTree(archive);
		}
		
		setAdapter(treeView, tree);
		
		this.onTabReselected(tab, ft);
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {		
	}


	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {	
		if(this.layout.getChildCount()>0){
            this.layout.removeAllViews();
        }
	}
	
	@SuppressWarnings("unused")
	private void setAdapter(final TreeView treeView, Tree<String> tree) {

		ArrayList<Tree<String>> intList = new ArrayList<Tree<String>>();

		intList.add(tree);

		final TreeAdapter<Tree<String>> _adapter = new TreeAdapter<Tree<String>>(
				this.activity, 0, intList) {

			@Override
			public View getTreeItemView(Tree<String> item, View convertView,
					ViewGroup parent) {
				TextView textView = new TextView(treeView.getContext());
				textView.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				textView.setPadding(5, 1, 4, 1);
				textView.setText(item.getValue() + "");
				return textView;
			}

			@Override
			public List<Tree<String>> getChildObjects(Tree<String> item) {
				ArrayList<Tree<String>> results = new ArrayList<Tree<String>>();

				if (item.getChildlen().isEmpty() == false) {
					List<Tree<String>> trees = item.getChildlen();
					if (trees != null)
						for (Tree<String> tree : trees) {
							results.add(tree);
						}
				}

				return results;
			}
		};

		treeView.setOnTreeItemClickListener(new OnTreeItemClickListener() {
			@SuppressWarnings("unchecked")
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					Object target) {
				Tree<String> tree = (Tree<String>) target;

				if (tree.getChildlen().isEmpty() == false) {
					_adapter.toggleExpand(tree);
				} else {
					ManacodeActivity activity = SourceTreeListener.this.activity;
					String path = getFullPath(tree);
					
					try {
						activity.createSourceView(SourceTreeListener.this.id,path);
					} catch (IOException e) {
						// TODO
					}
				}
			}
			
			private String getFullPath(Tree<String> tree) {
				String path = tree.getValue();
				
				if(tree.isRoot() == false) {
					Tree<String> parent = tree.getParent();
					
					return getFullPath(parent) + "/" + path;
				} else{
					return path;
				}
				
			}
		});

		int[] indentColors = new int[] { Color.WHITE, Color.GRAY, Color.CYAN,
				Color.MAGENTA, Color.GREEN };
		_adapter.setIndentWidth(10);

		treeView.setTreeAdapter(_adapter);

	}

	

}
